package putao.ui
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;
	
	import putao.ui.interf.IFocusComponent;
	
	public class PFocusManager
	{
		private static var _instance:PFocusManager;
		
		public var currentFocusObject:IFocusComponent;
		
		public var currentDictionary:DisplayObject;
		
		public var focusObjectDictionary:Dictionary = new Dictionary();
		
		public var stage:Stage;
		
		public function PFocusManager(inside:Inside)
		{
			
		}
		
		public static function getInstance(stage:Stage = null):PFocusManager
		{
			if(_instance == null)
			{
				_instance = new PFocusManager(new Inside);
			}
			if(_instance.stage == null && stage != null){
				_instance.setStage(stage);
			}
			return _instance;
		}
		
		/**
		 *
		 * 设置此焦点管理器对应的舞台
		 *  
		 * @param stage
		 * 
		 */		
		private function setStage(stage:Stage):void
		{
			this.stage = stage;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			currentDictionary = stage;
		}
		
		/**
		 *
		 * 获得当前焦点组所控制的组件数组
		 *  
		 * @return 
		 * 
		 */		
		public function getCurrentFocusArray():Vector.<IFocusComponent>
		{
			if(focusObjectDictionary[currentDictionary] == null){
				focusObjectDictionary[currentDictionary] = new Vector.<IFocusComponent>;
			}
			return focusObjectDictionary[currentDictionary] as Vector.<IFocusComponent>;
		}
		
		/**
		 *
		 * 设置当前焦点组
		 *  
		 * @param obj
		 * 
		 */		
		public function setFocusGroup(obj:DisplayObject):void
		{
			currentDictionary = obj;
		}
		
		public function deleteFocusGroup(obj:DisplayObject):void
		{
			if(focusObjectDictionary[obj] != null){
				delete focusObjectDictionary[obj];
			}
			setFocusGroup(stage);
		}
		
		/**
		 *
		 * 在当前组中添加新的组件
		 *  
		 * @param focusObject
		 * 
		 */		
		public function addFocusObject(focusObject:IFocusComponent):void
		{
			var focusV:Vector.<IFocusComponent> = getCurrentFocusArray();
			if(focusV == null){
				focusV = new Vector.<IFocusComponent>();
			}
			if(focusV.indexOf(focusObject) >= 0){
				focusV.splice(focusV.indexOf(focusObject), 1);
			}
			focusV.push(focusObject);
		}
		
		/**
		 *
		 * 从字典中删除指定的组件
		 *  
		 * @param focusObject
		 * 
		 */		
		public function deleteFocusObject(focusObject:IFocusComponent):void
		{
			for each(var focusObjects:Vector.<IFocusComponent> in focusObjectDictionary)
			{
				if(focusObjects.indexOf(focusObject) >= 0){
					focusObjects.splice(focusObjects.indexOf(focusObject), 1);
				}
			}
		}
		
		/**
		 *
		 * 焦点转移到下一个组件 
		 * 
		 */		
		public function nextFocus():void
		{
			var i:int = getCurrentFocusArray().indexOf(currentFocusObject);
			if(getCurrentFocusArray().length <= 0){
				return;
			}
			if(i < 0 ||i + 1 >= getCurrentFocusArray().length){
				i = 0;
			}else{
				i += 1;
			}
			if(currentFocusObject != null){
				currentFocusObject.unFocusHandler();
			}
			currentFocusObject = getCurrentFocusArray()[i];
			currentFocusObject.onFocusHandler();
		}
		
		private function onKeyDown(e:KeyboardEvent):void
		{
			if(e.keyCode == Keyboard.SPACE)
			{
				if(currentFocusObject){
					currentFocusObject.doSpaceDown();
				}
			}
		}
		
		private function onKeyUp(e:KeyboardEvent):void
		{
			if(e.keyCode == Keyboard.SPACE)
			{
				if(currentFocusObject){
					currentFocusObject.doSpaceUp();
				}
			}
			if(e.keyCode == Keyboard.TAB)
			{
				nextFocus();
			}
		}
	}
}class Inside{}